package com.supor.aiot.home.ui.device.config;

import android.content.Intent;
import android.os.Handler;

import com.android.baseconfig.common.utils.GsonUtil;
import com.android.baseconfig.common.utils.ToastUtils;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.base.BaseMVVMActivity;
import com.supor.aiot.common.entity.DeviceInfo;
import com.supor.aiot.common.entity.ProductType;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.common.helper.GlobalDataCache;
import com.supor.aiot.common.utils.NFCCheckUtils;
import com.supor.aiot.home.R;
import com.supor.aiot.home.databinding.ActivityNfcIntroduceBinding;
import com.supor.aiot.home.ui.device.AddDeviceViewModel;

public class NFCIntroduceActivity extends BaseMVVMActivity<AddDeviceViewModel, ActivityNfcIntroduceBinding> {

    private DeviceInfo device;
    private String productId = "";
    private boolean isBinding = false;

    @Override
    protected int resourceId() {
        return R.layout.activity_nfc_introduce;
    }

    @Override
    protected void initData() {
        super.initData();
        viewModel.getBindResult().observe(this, result -> {
            if (result instanceof Result.Success) {
                GlobalDataCache.getBindDevice();
                // 跳转小程序
                if (Constants.Type.BLENDER_Q8.equalsIgnoreCase(device.model)) {
                    if (AppAgent.h5Debug) {
                        AppAgent.startDebugTiny(this, Constants.App.PAGE_DEVICE, GsonUtil.getInstance().toJson(device), "");
                    } else {
                        AppAgent.startDevicePage(Constants.App.TINY_DEVICE_BLENDER, GsonUtil.getInstance().toJson(device));
                    }
                } else if (Constants.Type.TYPE_ELECTRIC_SAUCEPAN_C18.equalsIgnoreCase(device.model)) {
                    if (AppAgent.h5Debug) {
                        AppAgent.startDebugTiny(this, Constants.App.PAGE_DEVICE, GsonUtil.getInstance().toJson(device), "");
                    } else {
                        AppAgent.startDevicePage(Constants.App.TINY_DEVICE_ELECTRIC_SAUCEPAN_C18, GsonUtil.getInstance().toJson(device));
                    }
                }
                isBinding = false;

                // 销毁当前页面
                new Handler().postDelayed(() -> {
                    setResult(1);
                    finish();
                }, 1000);
            } else if (result instanceof Result.Error) {
                ToastUtils.getInstance().showCustomToast("绑定设备失败");
            }
        });
    }

    @Override
    protected void initView() {
        super.initView();
        Intent intent = getIntent();
        if (intent != null) {
            productId = intent.getExtras().getString("productId");
        }
        bindingView.toolbar.setIvLeftOnClickListener(v -> finish());
        bindingView.toolbar.setTvLeftOnClickListener(v -> finish());

        bindingView.btnAck.setOnClickListener(v -> {
            // 防抖
            if (!isBinding) {
                isBinding = true;

                device = new DeviceInfo();
                device.mac = NFCCheckUtils.generateUid(productId, false);
                device.pid = productId;

                for (ProductType productType : GlobalDataCache.productTypeList) {
                    for (ProductType.ProductModel child : productType.Children) {
                        if (productId.equalsIgnoreCase(child.productId)) {
                            device.crmModelName = child.crmModelName;
                            device.type = child.modelTypeId;
                            device.model = child.modelId;
                            device.productUrl = child.modelPicture;
                            device.productName = child.modelTypeName;
                            device.offStatusText = child.remark;
                            break;
                        }
                    }
                }

                viewModel.bindNFCDevice(device.mac, device.model);
            }
        });
    }
}